package com.atguigu.daijia.customer.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.loginCheck.LoginCheck;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.common.util.JwtUtil;
import com.atguigu.daijia.customer.mapper.CustomerInfoMapper;
import com.atguigu.daijia.customer.service.CustomerInfoService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {
    @Resource
    private WxMaService wxMaService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 返回登陆用户的 customerId
     *
     * @param code
     * @return
     */
    @Override
    public Long login(String code) {
        // 1. 根据code得到的openid
        String openId = null;
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            openId = sessionInfo.getOpenid();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        // 2. 判断得到的openid是否正确
        if (!StringUtils.hasLength(openId)) {
            throw new GuiguException(ResultCodeEnum.LOGIN_MOBLE_ERROR);
        }
        // 3. 判断openid是否存在于数据库中
        CustomerInfo customerInfo = query().eq("wx_open_id", openId).one();
        if (customerInfo == null) {
            // 3.2 不存在-新用户，将用户信息存入数据库
            customerInfo = new CustomerInfo();
            customerInfo.setWxOpenId(openId);
            customerInfo.setNickname("customer_"+ System.currentTimeMillis());
            customerInfo.setAvatarUrl("https://img1.baidu.com/it/u=728383910,3448060628&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800");
            save(customerInfo);
        }
        return customerInfo.getId();
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long customerId) {
        // 1. 获取customerId
        if (customerId == null) {
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
        }
        // 2. 获取customer信息
        CustomerInfo customerInfo = getById(customerId);
        if (customerInfo == null) {
            throw new GuiguException(ResultCodeEnum.LOGIN_MOBLE_ERROR);
        }
        // 3. 填充CustomerLoginVo属性
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
        BeanUtils.copyProperties(customerInfo, customerLoginVo);
        customerLoginVo.setIsBindPhone(true);
        // 4. 返回填充CustomerLoginVo
        return customerLoginVo;
    }
}
